using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Pegasus.Reflection
{
    /// <summary>
    /// Base class which can display its fields.
    /// </summary>
    [Serializable]
    public class DisplayableObject
    {
        /// <summary>
        /// Parameterless constructor needed for Serialization.
        /// </summary>
        public DisplayableObject() { }

        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString()
        {
            try
            {
                StringBuilder sb = new StringBuilder();

                Type type = this.GetType();
                FieldInfo[] fields = type.GetFields();
                foreach( FieldInfo field in fields )
                {
                    sb.AppendFormat( "{0}:{1}{2},", field.Name, field.GetValue( this ), Environment.NewLine );
                }

                return sb.ToString();
            }
            catch
            {
                return "unable to ToString object";
            }
        }
    }
}
